home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / tools / cpx_acc / makecpx / sample / sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  13.6 KB  |  488 lines

  1. /*==========================================================================
  2.  * FILE: SAMPLE.C
  3.  *==========================================================================
  4.  * DATE:     November 8, 1990
  5.  * DESCRIPTION: SAMPLE CPX demonstrating the use of the popup and slider
  6.  *        functions in a FORM-TYPE CPX.
  7.  * COMPILER:     Mark Williams C
  8.  */
  9.  
  10. /* INCLUDE FILES
  11.  *==========================================================================
  12.  */
  13. #include <stdio.h>
  14. #include <portab.h>
  15. #include <osbind.h>
  16. #include <gemdefs.h>
  17. #include <obdefs.h>
  18. #include <string.h>
  19.  
  20. #include "sample.h"
  21. #include "sample.rsh"
  22.  
  23. #include "cpxdata.h"           /* Our CPX Data Structures */    
  24.  
  25.  
  26. /* PROTOTYPES
  27.  *==========================================================================
  28.  */
  29. CPXINFO    *cpx_init();
  30. BOOLEAN cpx_call();
  31.  
  32. void    initialize();
  33. int    handle_button();
  34. void    VDraw();
  35. void    HDraw();
  36.  
  37. int    Pop_Handle();
  38.  
  39.  
  40. /* DEFINES
  41.  *==========================================================================
  42.  */
  43. #define NUM_ITEMS    5        /* # of items for the popup.     */
  44.  
  45. #define MAX_ITEMS    12        /* Upper limit for slider value. */
  46. #define MIN_ITEMS    0        /* Lower limit for slider value  */
  47.  
  48. #define AAAA        0        /* Start with the first index     */
  49. #define INIT_VALUE    6        /* Initial value for sliders     */
  50.  
  51.  
  52.  
  53. /* EXTERNALS
  54.  *==========================================================================
  55.  */
  56.  
  57.  
  58. /* GLOBALS
  59.  *==========================================================================
  60.  */
  61. XCPB *xcpb;                  /* XControl Parameter Block       */
  62. CPXINFO cpxinfo;              /* CPX Information Structure      */
  63.  
  64. OBJECT *tree;                   /* Our resource object tree      */
  65. GRECT    desk;                  /* GRECT of the desktop work area */
  66.  
  67. char blanks[5][30];              /* Used for Pop_Up String Builds  */
  68. char *blank[5];        
  69.  
  70. char *menu_items[] = { "AAAA",          /* The text for the popup button. */
  71.                "BBBB",
  72.                "CCCC",
  73.                "DDDD",
  74.                "EEEE"
  75.                  };
  76.                  
  77. BOOLEAN click_flag = FALSE;    /* Double click flag */
  78. int  cur_item;            /* Current popup menu item ( zero based )*/
  79.  
  80. char vcur_text[3];        /* Text Strings for current slider values*/
  81. char hcur_text[3];
  82.  
  83. int  vcur_item;            /* Current Slider Values          */
  84. int  hcur_item;
  85.  
  86. MFORM Mbuffer;            /* Mouse background image storage     */
  87.  
  88. int  errno;
  89.  
  90. /***** GLOBAL VARIABLES FOR VDI ***/
  91. WORD    contrl[12];
  92. WORD    intin[128];
  93. WORD    ptsin[128];
  94. WORD    intout[128];
  95. WORD    ptsout[128];
  96.  
  97.  
  98. /* FUNCTIONS
  99.  *==========================================================================
  100.  */
  101.  
  102. /* cpx_init()
  103.  *==========================================================================
  104.  * cpx_init() is where a CPX is first initialized.
  105.  * There are TWO parts.
  106.  *
  107.  * PART ONE: cpx_init() is called when the CPX is first 'booted'. This is
  108.  *         where the CPX should read in its defaults and then set the
  109.  *         hardware based on those defaults.  
  110.  *
  111.  * PART TWO: The other time cpx_init() is called is when the CPX is being
  112.  *         executed.  This is where the resource is fixed up and current
  113.  *         cpx variables are updated from the hardware.  In addition,
  114.  *         this is where the CPXINFO structure should be filled out.
  115.  *
  116.  * IN:  XCPB    *Xcpb:    Pointer to the XControl Parameter Block
  117.  * OUT: CPXINFO  *ptr:    Pointer to the CP Information Structure
  118.  */            
  119. CPXINFO
  120. *cpx_init( Xcpb )
  121. XCPB *Xcpb;
  122. {
  123.     xcpb = Xcpb;
  124.  
  125.     appl_init();
  126.     
  127.      /* Only those CPXs with their 'Booting' flag or 'Set-only' flag
  128.       * set will be initialized here.
  129.       *
  130.       * Read in your defaults from 'save_vars' and slam them into
  131.       * the hardware where appropriate.
  132.       *
  133.       * Return TRUE to tell XCONTROL that the header should be retained
  134.       * so that the cpx may be executed.
  135.       *
  136.       * Return FALSE to tell XCONTROL NOT to retain our header.
  137.       * An example of this would be a 'Set-Only' CPX.
  138.       */
  139.     if( xcpb->booting )
  140.         return( ( CPXINFO *)TRUE );  
  141.     else
  142.     {
  143.       /* Fixup the resource...
  144.        * The flag prevents rsh_fix() from being called more than once.
  145.        * If executing from disk, rsh_fix() is called every time. However,
  146.        * If the CPX is 'resident', rsh_fix() will be called only once.
  147.        */
  148.         
  149.       if( !xcpb->SkipRshFix )
  150.            (*xcpb->rsh_fix)( NUM_OBS, NUM_FRSTR, NUM_FRIMG, NUM_TREE,
  151.                             rs_object, rs_tedinfo, rs_strings, rs_iconblk,
  152.                             rs_bitblk, rs_frstr, rs_frimg, rs_trindex,
  153.                             rs_imdope );
  154.            
  155.       tree   = ( OBJECT *)rs_trindex[ MAINTREE ];
  156.                         
  157.       /* Initialize the CPXINFO structure */           
  158.       cpxinfo.cpx_call   = cpx_call;
  159.       cpxinfo.cpx_draw   = NULL;
  160.       cpxinfo.cpx_wmove  = NULL;
  161.       cpxinfo.cpx_timer  = NULL;
  162.       cpxinfo.cpx_key    = NULL;
  163.       cpxinfo.cpx_button = NULL;
  164.       cpxinfo.cpx_m1      = NULL;
  165.       cpxinfo.cpx_m2     = NULL;
  166.       cpxinfo.cpx_hook   = NULL;
  167.       cpxinfo.cpx_close  = NULL;
  168.       
  169.       return( &cpxinfo );
  170.     }
  171. }
  172.  
  173.  
  174.  
  175.  
  176. /* cpx_call()
  177.  *==========================================================================
  178.  * Called when a CPX is invoked AFTER the cpx_init() call has been
  179.  * completed.  The function is passed a rectangle describing the current
  180.  * work area of the XControl window.  This allows a CPX to set up for
  181.  * a user interaction and optionally call the custom xform_do() routine
  182.  * to handle its user interface.
  183.  *
  184.  *
  185.  * IN: GRECT *rect:    Ptr to a GRECT that describes the current work
  186.  *            area of the XControl window.
  187.  *
  188.  * OUT:
  189.  *   FALSE:     The CPX has exited and no more messages are
  190.  *        needed.  XControl will either return to its
  191.  *        main menu or close its windows.
  192.  *        This is used by XForm_do() type CPXs.
  193.  *
  194.  *   TRUE:    The CPX requests that XCONTROL continue to
  195.  *        send AES messages.  This is used by EVENT-TYPE CPXs.
  196.  */
  197. BOOLEAN
  198. cpx_call( rect )
  199. GRECT *rect;
  200. {
  201.      int  button;
  202.      int  quit = 0;
  203.      WORD msg[8];
  204.      
  205.      ObX( ROOT ) = rect->g_x;
  206.      ObY( ROOT ) = rect->g_y;
  207.  
  208.      initialize();
  209.      
  210.      wind_get( 0, WF_WORKXYWH, &desk.g_x, &desk.g_y, &desk.g_w, &desk.g_h );
  211.      objc_draw( tree, ROOT, MAX_DEPTH, PTRS( rect ) );
  212.      do
  213.      {
  214.     button = (*xcpb->Xform_do)( tree, 0, msg );
  215.     quit = handle_button( button, msg );
  216.  
  217.      }while( !quit);
  218.      return( FALSE );
  219. }
  220.  
  221.  
  222.  
  223. /* initialize()
  224.  *==========================================================================
  225.  * Initialize the slider and popup values and calculate 
  226.  * the slider positions.
  227.  *
  228.  * IN:  none
  229.  *
  230.  * OUT: none
  231.  */
  232. void
  233. initialize()
  234. {
  235.    cur_item = AAAA;
  236.    vcur_item = hcur_item = INIT_VALUE;
  237.  
  238.    sprintf( vcur_text, "%d", vcur_item );
  239.    TedText( SLIDEV ) = vcur_text;
  240.    ( *xcpb->Sl_y)( tree, BASEV, SLIDEV, vcur_item, MAX_ITEMS, MIN_ITEMS, NULLFUNC );
  241.  
  242.    sprintf( hcur_text, "%d", hcur_item );
  243.    TedText( SLIDEH ) = hcur_text;
  244.    ( *xcpb->Sl_x)( tree, BASEH, SLIDEH, hcur_item, MIN_ITEMS, MAX_ITEMS, NULLFUNC );
  245.    
  246. }
  247.  
  248.  
  249.  
  250.  
  251. /* handle_button()
  252.  *==========================================================================
  253.  * Perform button click handling
  254.  *
  255.  * IN:  int button: AES object clicked on.
  256.  *    WORD: *msg: message array returned from the event_multi().
  257.  *
  258.  * OUT: int quit:   Returns value to cpx_call().
  259.  *            FALSE - don't quit cpx
  260.  *            TRUE  - quit the cpx and return to XControl.
  261.  *
  262.  */
  263. int
  264. handle_button( button, msg )
  265. int button;
  266. WORD *msg;
  267. {
  268.    int   quit = FALSE;
  269.    int   obj;
  270.    MRETS mk;
  271.    int   ox,oy;
  272.    GRECT rect;
  273.    
  274.    /* Check if we have a double click item */   
  275.    if( ( button != -1 ) && ( button & 0x8000 ) )
  276.    {
  277.       click_flag = TRUE;
  278.       button &= 0x7FFF;      
  279.    }   
  280.    
  281.    switch( button )
  282.    {
  283.      case QUIT:  quit = TRUE;
  284.               Deselect( QUIT );
  285.               break;
  286.                
  287.      case POPUP: obj = Pop_Handle( tree, POPUP, menu_items, NUM_ITEMS, &cur_item, IBM );
  288.  
  289.          if( obj != -1 )
  290.          {
  291.            cur_item = obj;
  292.            TedText( POPUP ) = menu_items[ cur_item ];
  293.          }
  294.          Deselect( POPUP );
  295.  
  296.          rect = ObRect( POPUP );
  297.          objc_offset( tree, POPUP, &rect.g_x, &rect.g_y );
  298.          objc_draw( tree, POPUP, MAX_DEPTH, ELTS( rect ) );
  299.          break;
  300.          
  301.          
  302.          
  303.      case UP:       (*xcpb->Sl_arrow)( tree, BASEV, SLIDEV, UP, -1,
  304.                                      MAX_ITEMS, MIN_ITEMS, &vcur_item,
  305.                                      VERTICAL, VDraw );
  306.            break;
  307.      
  308.      case DOWN:       (*xcpb->Sl_arrow)( tree, BASEV, SLIDEV, DOWN, 1,
  309.                                  MAX_ITEMS, MIN_ITEMS, &vcur_item,
  310.                                  VERTICAL, VDraw );
  311.                   break;          
  312.  
  313.      case BASEV:   graf_mkstate( &mk.x, &mk.y, &mk.buttons, &mk.kstate );
  314.                    objc_offset( tree, SLIDEV, &ox, &oy );
  315.                    ox = (( mk.y < oy ) ? ( -2 ) : ( 2 ) );
  316.                    (*xcpb->Sl_arrow)( tree, BASEV, SLIDEV, -1, ox,
  317.                                        MAX_ITEMS, MIN_ITEMS, &vcur_item,
  318.                                        VERTICAL, VDraw );
  319.            break;
  320.  
  321.      case SLIDEV:  (*xcpb->MFsave)( MFSAVE, &Mbuffer );
  322.                graf_mouse( FLAT_HAND, 0L );
  323.                    (*xcpb->Sl_dragy)( tree, BASEV, SLIDEV, MAX_ITEMS,
  324.                                  MIN_ITEMS, &vcur_item, VDraw );
  325.                (*xcpb->MFsave)( MFRESTORE, &Mbuffer );
  326.                 break;
  327.                
  328.  
  329.  
  330.  
  331.      case LEFT:       (*xcpb->Sl_arrow)( tree, BASEH, SLIDEH, LEFT, -1,
  332.                              MIN_ITEMS, MAX_ITEMS, &hcur_item,
  333.                              HORIZONTAL, HDraw );
  334.            break;
  335.            
  336.      case RIGHT:   (*xcpb->Sl_arrow)( tree, BASEH, SLIDEH, RIGHT, 1,
  337.                              MIN_ITEMS, MAX_ITEMS, &hcur_item,
  338.                              HORIZONTAL, HDraw );
  339.                   break;          
  340.  
  341.      case BASEH:   graf_mkstate( &mk.x, &mk.y, &mk.buttons, &mk.kstate );
  342.                    objc_offset( tree, SLIDEH, &ox, &oy );
  343.                    oy = (( mk.x < ox ) ? ( -2 ) : ( 2 ) );
  344.                    (*xcpb->Sl_arrow)( tree, BASEH, SLIDEH, -1, oy,
  345.                                        MIN_ITEMS, MAX_ITEMS, &hcur_item,
  346.                                        HORIZONTAL, HDraw );
  347.                 break;
  348.                 
  349.      case SLIDEH:  (*xcpb->MFsave)( MFSAVE, &Mbuffer );
  350.                graf_mouse( FLAT_HAND, 0L );
  351.                    (*xcpb->Sl_dragx)( tree, BASEH, SLIDEH, MIN_ITEMS,
  352.                                  MAX_ITEMS, &hcur_item, HDraw );
  353.                (*xcpb->MFsave)( MFRESTORE, &Mbuffer );
  354.                 break;
  355.      
  356.                                                                   
  357.      default:     if( button == -1 )
  358.               {
  359.                 switch( msg[0] )
  360.                 {
  361.                   case AC_CLOSE:  
  362.                   case WM_CLOSED: quit = TRUE;
  363.                      break;
  364.  
  365.                   case WM_REDRAW: 
  366.              case CT_KEY:    
  367.                           break;
  368.                   default:
  369.                           break;
  370.                 }
  371.               }
  372.               break;
  373.    }
  374.    return( quit );
  375. }
  376.  
  377.  
  378.  
  379. /* VDraw()
  380.  *==========================================================================
  381.  * Convert the vertical slider value to a string, update the rcs 
  382.  * structure, and redraw the string.
  383.  * 
  384.  * GLOBAL: int vcur_item:
  385.  *       char vcur_text[]:
  386.  *       OBJECT *tree:
  387.  *
  388.  * IN:  none
  389.  * OUT: none
  390.  */
  391. void
  392. VDraw()
  393. {
  394.   GRECT rect;
  395.   
  396.   sprintf( vcur_text, "%d", vcur_item );
  397.   TedText( SLIDEV ) = vcur_text;
  398.   rect = ObRect( SLIDEV );
  399.   objc_offset( tree, SLIDEV, &rect.g_x, &rect.g_y );
  400.   objc_draw( tree, SLIDEV, MAX_DEPTH, ELTS( rect ) );   
  401. }
  402.  
  403.  
  404.  
  405.  
  406. /* HDraw()
  407.  *==========================================================================
  408.  * Convert the horizontal slider value to a string, update the rcs 
  409.  * structure, and redraw the string.
  410.  * 
  411.  * GLOBAL: int hcur_item:
  412.  *       char hcur_text[]:
  413.  *       OBJECT *tree:
  414.  *
  415.  * IN:  none
  416.  * OUT: none
  417.  */
  418. void
  419. HDraw()
  420. {
  421.   GRECT rect;
  422.   
  423.   sprintf( hcur_text, "%d", hcur_item );
  424.   TedText( SLIDEH ) = hcur_text;
  425.   rect = ObRect( SLIDEH );
  426.   objc_offset( tree, SLIDEH, &rect.g_x, &rect.g_y );
  427.   objc_draw( tree, SLIDEH, MAX_DEPTH, ELTS( rect ) );
  428. }
  429.  
  430.  
  431.  
  432.  
  433. /* Pop_Handle()
  434.  *==========================================================================
  435.  * Setup the strings for the popup menu. We want the strings to have 2
  436.  * spaces in front of each string and spaces after the string up to
  437.  * the length + 1 of the longest string.
  438.  * 
  439.  * IN:     OBJECT *tree:      our RCS tree
  440.  *     int    button:    The popup object we clicked on to get here.
  441.  *    char   *items[]:  Pointer array to the text for the menu.
  442.  *    int    num_items: The number of items in the text array.
  443.  *      int    *default_item:  The current item to check mark ( zero based)
  444.  *                   Set to -1 if no item is to be checked.
  445.  *    int    font_size: AES font size - currently the large font
  446.  *              is only supported.
  447.  *
  448.  * OUT: # of the menu item selected ( zero based ).
  449.  *               - OR -
  450.  *     -1 No item selected.
  451.  */
  452. int
  453. Pop_Handle( tree, button, items, num_items, default_item, font_size )
  454. OBJECT  *tree;
  455. int     button;
  456. char     *items[];
  457. int    num_items;
  458. int    *default_item;
  459. int    font_size;
  460. {
  461.    GRECT butn, world;
  462.    
  463.    int  i;
  464.    int  obj;
  465.  
  466.    butn = ObRect( button );
  467.    objc_offset( tree, button, &butn.g_x, &butn.g_y );
  468.    world = ObRect( ROOT );
  469.    objc_offset( tree, ROOT, &world.g_x, &world.g_y ); 
  470.    
  471.    /* This will put 2 spaces before each string and 1 space after
  472.     * each string. For variable length strings, you'll have to 
  473.     * pad spaces up to the length of the longest string plus 1.
  474.     */
  475.    for( i = 0; i < num_items; i++ )
  476.    {
  477.       strcpy( &blanks[i][0], "  ");
  478.       strcat( &blanks[i][0], items[ i ]);
  479.       strcat( &blanks[i][0], "  ");
  480.       blank[i] = &blanks[i][0];
  481.    }
  482.    
  483.    obj = (*xcpb->Popup)( blank, num_items, *default_item, font_size, &butn, &world );
  484.    return( obj );
  485. }
  486.  
  487.  
  488.